package arrays;

import java.util.Arrays;

public class Array02 {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9,10};
       Array02.reOrderArray(array);
        System.out.println(Arrays.toString(array));
    }
    //快慢指针法：时间复杂度O(n)，空间复杂度O(1)
    public static int[] reOrderArray(int[] array) {
        if (array == null || array.length == 0) return array;

        int slow = 0; //慢指针指向当前奇数末尾
        for (int fast = 0; fast < array.length; fast++) {
            //当前元素为奇数
            if (array[fast] % 2 == 1) {
                int temp = array[fast];
                for (int i = fast; i > slow; i--) {
                    array[i] = array[i - 1];
                }
                //当前元素为偶数，slow++
                array[slow] = temp;
                slow++;
            }
        }
        return array;
    }
}
